package cn.wsalix.msg.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.SimpleServiceImpl;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;
import cn.wsalix.msg.entity.MessDetails;
import cn.wsalix.msg.entity.Message;
import cn.wsalix.msg.form.MessageForm;
import cn.wsalix.msg.repository.PushRepository;
import cn.wsalix.msg.service.MessageService;
import cn.wsalix.msg.service.PushService;
import cn.wsalix.observable.service.SendService;
import cn.wsalix.user.entity.Mine;
import cn.wsalix.user.form.MessDetailsForm;
import cn.wsalix.user.service.MineService;

@Service("pushService")
public class PushServiceImpl extends
		SimpleServiceImpl<MessDetailsForm, PushRepository, MessDetails> implements
		PushService {
	@Autowired
	private PushRepository pushRepository;
	@Autowired
	private MessageService messageService;
	@Autowired
	private MineService myProductService;
	// @Autowired
	// private AppConfig appConfig;
	/*@Autowired
	private PushProxy pushProxy;*/

	@Override
	public String module() {
		return "/shop/push";
	}

	@Override
	public PushRepository getRepository() {
		return pushRepository;
	}

	@Override
	public Class<MessDetails> getEntityClazz() {
		return MessDetails.class;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(MessDetailsForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getName() != null && !form.getName().trim().equals("")) {
			filters.put("name",
					new SearchFilter("name", Operator.LIKE, form.getName()));
		}
		if (form.getMessageId() != null && form.getMessageId() != 0) {
			filters.put("message.id", new SearchFilter("message.id",
					Operator.EQ, form.getMessageId()));
		}
		return filters;
	}

	@Transactional
	@Override
	public void update(Observable o, Object arg) {
		if (o instanceof SendService) {
			SendService sendService = (SendService) o;
			if (arg instanceof MessageForm) {
				MessageForm form = (MessageForm) arg;
				Message message = sendService.getMessage();
				if (message.isPush()) {
					switch (message.getType()) {
					case order:
						/*
						 * push.setContent("{'type':'product','id':'" +
						 * message.getFkId() + "','content':'" +
						 * message.getTitle() + "'}");
						 */
						break;
					case product:
						List<Mine> resLst = myProductService
								.findByProductId(message.getFkId());
						for (Mine myProduct : resLst) {
							/*PushForm push = new PushForm();
							push.setTitle(message.getTitle());
							// push.setPushType(appConfig.getPushType());
							push.setTag(myProduct.getUser().getUsername());
							push.setContent("{'type':'product','id':'"
									+ message.getFkId() + "','content':'"
									+ message.getTitle() + "'}");
							MessDetails messDetails = BeanMapper.map(push, MessDetails.class);
							super.add(messDetails);
							pushProxy.push(push);*/
							// myProduct.setMsgNum(myProduct.getMsgNum() + 1);
							// myProductService.edit(myProduct);
						}

						break;
					case tuan:
						break;
					default:
						break;
					}

				}
			}
		}

	}

	@Override
	public List<MessDetails> findByMess(Message message) {
		MessDetailsForm form = new MessDetailsForm();
		form.setMessageId(message.getId());
		return findByForm(form);
	}

}
